package cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodName;

import cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodDetail.vo.BizRiskMethodDetailExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodDetail.vo.BizRiskMethodDetailRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskMethodName.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.riskMethodDetail.BizRiskMethodDetailConvert;
//import cn.iocoder.yudao.module.biz.convert.risk.riskMethodDetail.BizRiskMethodDetailConvertImpl;
import cn.iocoder.yudao.module.biz.convert.risk.riskMethodName.BizRiskMethodNameConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskMethodDetail.BizRiskMethodDetailDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskMethodName.BizRiskMethodNameDO;
import cn.iocoder.yudao.module.biz.service.risk.riskMethodDetail.BizRiskMethodDetailService;
import cn.iocoder.yudao.module.biz.service.risk.riskMethodName.BizRiskMethodNameService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 评级方法")
@RestController
@RequestMapping("/biz/riskMethodName")
@Validated
public class BizRiskMethodNameController {

    @Resource
    private BizRiskMethodNameService riskMethodNameService;
    @Resource
    private BizRiskMethodDetailService riskMethodDetailService;

    @PostMapping("/create")
    @ApiOperation("创建评级方法")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:create')")
    public CommonResult<String> createRiskMethodName(@Valid @RequestBody BizRiskMethodNameCreateReqVO createReqVO) {
        return success(riskMethodNameService.createRiskMethodName(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新评级方法")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:update')")
    public CommonResult<Boolean> updateRiskMethodName(@Valid @RequestBody BizRiskMethodNameUpdateReqVO updateReqVO) {
        riskMethodNameService.updateRiskMethodName(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除评级方法")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:delete')")
    public CommonResult<Boolean> deleteRiskMethodName(@RequestParam("id") String id) {
        riskMethodNameService.deleteRiskMethodName(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得评级方法")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<BizRiskMethodNameRespVO> getRiskMethodName(@RequestParam("id") String id) {
        BizRiskMethodNameDO riskMethodName = riskMethodNameService.getRiskMethodName(id);
        BizRiskMethodNameRespVO vo = BizRiskMethodNameConvert.INSTANCE.convert(riskMethodName);
        BizRiskMethodDetailExportReqVO v = new BizRiskMethodDetailExportReqVO();
        v.setMethodNameId(vo.getId());
        vo.setDetailDOList(riskMethodDetailService.getRiskMethodDetailList(v));
        return success(vo);
    }

    @GetMapping("/list")
    @ApiOperation("获得评级方法列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<List<BizRiskMethodNameRespVO>> getRiskMethodNameList(@RequestParam("ids") Collection<String> ids) {
        List<BizRiskMethodNameDO> list = riskMethodNameService.getRiskMethodNameList(ids);
        return success(BizRiskMethodNameConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getAllRiskMethodName")
    @ApiOperation("获得评级方法列表")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<List<BizRiskMethodNameRespVO>> getAllRiskMethodName() {
        List<BizRiskMethodNameDO> list = riskMethodNameService.getAllRiskMethodName();
        return success(BizRiskMethodNameConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得评级方法分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<PageResult<BizRiskMethodNameRespVO>> getRiskMethodNamePage(@Valid BizRiskMethodNamePageReqVO pageVO) {
        PageResult<BizRiskMethodNameDO> pageResult = riskMethodNameService.getRiskMethodNamePage(pageVO);
        return success(BizRiskMethodNameConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出评级方法 Excel")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:export')")
    @OperateLog(type = EXPORT)
    public void exportRiskMethodNameExcel(@Valid BizRiskMethodNameExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizRiskMethodNameDO> list = riskMethodNameService.getRiskMethodNameList(exportReqVO);
        // 导出 Excel
        List<BizRiskMethodNameExcelVO> datas = BizRiskMethodNameConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "评级方法主.xls", "数据", BizRiskMethodNameExcelVO.class, datas);
    }

    @GetMapping("/getRiskMethodDetailList")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<List<BizRiskMethodDetailRespVO>> getRiskMethodDetailList(@RequestParam("methodNameId") String methodNameId,@RequestParam("type") String type) {
        List<BizRiskMethodDetailDO> list = riskMethodDetailService.getRiskMethodDetailList(methodNameId,type);
        return success(BizRiskMethodDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getRiskMethodDetailType")
    @PreAuthorize("@ss.hasPermission('biz:risk-method-name:query')")
    public CommonResult<List<BizRiskMethodDetailRespVO>> getRiskMethodDetailType(@RequestParam("methodNameId") String methodNameId) {
        List<BizRiskMethodDetailDO> list = riskMethodDetailService.getRiskMethodDetailType(methodNameId);
        return success(BizRiskMethodDetailConvert.INSTANCE.convertList(list));
    }
}
